package com.cskaoyan.service.issue;

import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.issue.Issue;
import com.cskaoyan.bean.issue.IssueExample;
import com.cskaoyan.bean.issue.bo.QueryIssueBo;
import com.cskaoyan.bean.issue.bo.UpdateIssueBo;
import com.cskaoyan.bean.issue.vo.CreateIssueVo;
import com.cskaoyan.bean.issue.vo.IssueDataVo;
import com.cskaoyan.bean.issue.vo.IssueVo;
import com.cskaoyan.mapper.IssueMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 创建日期: 2022/05/06 21:08
 *
 * @author JinLu Zhou
 */
@Service
public class IssueServiceImpl implements IssueService {

    @Autowired
    IssueMapper issueMapper;

    @Override
    public BaseRespVo issueList(QueryIssueBo queryIssueBo) {
        IssueExample issueExample = new IssueExample();
        issueExample.setOrderByClause(queryIssueBo.getSort() + " " + queryIssueBo.getOrder());
        IssueExample.Criteria criteria = issueExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        if (queryIssueBo.getQuestion() != null) {
            criteria.andQuestionLike("%" + queryIssueBo.getQuestion() + "%");
        }
        PageHelper.startPage(queryIssueBo.getPage(), queryIssueBo.getLimit());
//执行查询过程中拼接分页信息
        List<Issue> issues = issueMapper.selectByExample(issueExample);
        PageInfo<Issue> pageInfo = new PageInfo<>(issues);
        long total = pageInfo.getTotal();
        int pages = (int) total / queryIssueBo.getLimit() + 1;

        ArrayList<IssueVo> issueVos = new ArrayList<IssueVo>();
        for (Issue issue : issues) {
            IssueVo issueVo = new IssueVo();
            issueVo.set(issue);
            issueVos.add(issueVo);
        }
        IssueDataVo issueDataVo = new IssueDataVo();
        issueDataVo.setLimit(queryIssueBo.getLimit());
        issueDataVo.setPage(queryIssueBo.getPage());
        issueDataVo.setPages(pages);
        issueDataVo.setTotal((int) total);
        issueDataVo.setList(issueVos);

        BaseRespVo<IssueDataVo> issueDataVoBaseRespVo = new BaseRespVo<>();
        issueDataVoBaseRespVo.setErrno(0);
        issueDataVoBaseRespVo.setErrmsg("成功");
        issueDataVoBaseRespVo.setData(issueDataVo);
        return issueDataVoBaseRespVo;
    }

    @Override
    public BaseRespVo createIssue(String answer, String question) {
        Issue issue = new Issue();
        issue.setAnswer(answer);
        issue.setQuestion(question);
        issue.setDeleted(false);
        issue.setAddTime(new Date());
        issue.setUpdateTime(new Date());
        issueMapper.insertSelective(issue);

        CreateIssueVo createIssueVo = new CreateIssueVo();
        createIssueVo.set(issue);

        BaseRespVo<CreateIssueVo> createIssueVoBaseRespVo = new BaseRespVo<>();
        createIssueVoBaseRespVo.setErrno(0);
        createIssueVoBaseRespVo.setErrmsg("成功");
        createIssueVoBaseRespVo.setData(createIssueVo);
        return createIssueVoBaseRespVo;
    }

    @Override
    @Transactional
    public BaseRespVo updateIssue(UpdateIssueBo updateIssueBo) {
        IssueExample issueExample = new IssueExample();
        IssueExample.Criteria criteria = issueExample.createCriteria();
        criteria.andIdEqualTo(updateIssueBo.getId());
        Issue issue = new Issue();
        issue.setAnswer(updateIssueBo.getAnswer());
        issue.setUpdateTime(new Date());
        issue.setQuestion(updateIssueBo.getQuestion());
        issueMapper.updateByExampleSelective(issue, issueExample);

        Issue issue1 = issueMapper.selectByPrimaryKey(updateIssueBo.getId());

        CreateIssueVo createIssueVo = new CreateIssueVo();
        createIssueVo.set(issue1);

        BaseRespVo<CreateIssueVo> createIssueVoBaseRespVo = new BaseRespVo<>();
        createIssueVoBaseRespVo.setErrno(0);
        createIssueVoBaseRespVo.setErrmsg("成功");
        createIssueVoBaseRespVo.setData(createIssueVo);

        return createIssueVoBaseRespVo;
    }

    @Override
    public BaseRespVo deleteIssue(UpdateIssueBo updateIssueBo) {
        IssueExample issueExample = new IssueExample();
        IssueExample.Criteria criteria = issueExample.createCriteria();
        criteria.andIdEqualTo(updateIssueBo.getId());
        Issue issue = new Issue();
        issue.setDeleted(true);
        issue.setUpdateTime(new Date());
        issueMapper.updateByExampleSelective(issue, issueExample);
        BaseRespVo baseRespVo = new BaseRespVo();
        baseRespVo.setErrmsg("成功");
        baseRespVo.setErrno(0);
        return baseRespVo;
    }
}
